package com.express.controller;


import com.express.auto.JSONAuthentication;
import com.express.entity.ExCar;
import com.express.entity.ExOrder;
import com.express.mapper.ExCarMapper;
import com.express.response.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author express
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/ex-car")
public class ExCarController extends JSONAuthentication {

    @Autowired
    private ExCarMapper exCarMapper;

    @ApiOperation(value = "添加货车")
    @ApiImplicitParam(value = "整个货车参数")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody ExCar exCar)throws IOException, ServletException {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        exCar.setCreatTime(timestamp);
        int i=exCarMapper.insert(exCar);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }


    }
    @ApiOperation(value = "添加货车")
    @ApiImplicitParam(value = "货车id")
    @PostMapping("/del")
    public void delCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int carId) throws IOException, ServletException {
        int i = exCarMapper.deleteById(carId);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

}
    @ApiOperation(value = "修改货车")
    @ApiImplicitParam(value = "货车id")
    @PostMapping("/upd")
    public void updCar(HttpServletRequest request, HttpServletResponse response, @RequestBody ExCar exCar) throws IOException, ServletException {
        int i = exCarMapper.updateById(exCar);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "查询货车")
    @ApiImplicitParam(value = "货车id")
    @PostMapping("/look")
    public void lookCar(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Result ok = Result.ok().data("ok",exCarMapper.findAll());
        this.WriteJSON(request, response, ok);
        }



}

